{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import cv2\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# image is ndarray\n",
    "image.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 先转换类型再处理\n",
    "test = (np.squeeze(image)*255).astype(np.uint8)\n",
    "print(test.dtype)\n",
    "test = cv2.resize(test, (300,300))\n",
    "# threshold = 0.5\n",
    "test = cv2.threshold(test, 127,255,cv2.THRESH_BINARY)[1]\n",
    "cv2.imwrite('test_uint8.jpg',test)\n",
    "plt.imshow(test, cmap='gray')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 先处理再转换类型\n",
    "test = np.squeeze(image)\n",
    "print(test.dtype)\n",
    "test = cv2.resize(test, (300,300))\n",
    "# threshold = 0.5\n",
    "test = cv2.threshold(test, 0.5, 1,cv2.THRESH_BINARY)[1]\n",
    "# convert\n",
    "test = (test*255).astype(np.uint8)\n",
    "# cv2.imwrite('test_uint8.jpg',test)\n",
    "plt.imshow(test, cmap='gray')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
